package com.dianping.cat.consumer.problem.model.transform;

import com.dianping.cat.consumer.problem.model.IVisitor;
import com.dianping.cat.consumer.problem.model.entity.Duration;
import com.dianping.cat.consumer.problem.model.entity.Entity;
import com.dianping.cat.consumer.problem.model.entity.Entry;
import com.dianping.cat.consumer.problem.model.entity.GraphTrend;
import com.dianping.cat.consumer.problem.model.entity.JavaThread;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.entity.Segment;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
    this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static ProblemReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static ProblemReport parse(InputStream in) {
/*  33 */     DefaultNativeParser parser = new DefaultNativeParser(in);
/*  34 */     ProblemReport problemReport = new ProblemReport();

    try {
/*  37 */       problemReport.accept(parser);
/*  38 */     } catch (RuntimeException e) {
/*  39 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  42 */         throw e;
      }
    }

/*  46 */     parser.m_linker.finish();
/*  47 */     return problemReport;
  }



  public void visitDuration(Duration duration) {
    byte tag;
/*  54 */     while ((tag = readTag()) != -1) {
/*  55 */       visitDurationChildren(duration, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDurationChildren(Duration duration, int _field, int _type) {
/*  60 */     switch (_field) {
      case 1:
/*  62 */         duration.setValue(readInt());
        break;
      case 2:
/*  65 */         duration.setCount(readInt());
        break;
      case 3:
/*  68 */         if (_type == 1) {
/*  69 */           duration.addMessage(readString()); break;
/*  70 */         }  if (_type == 2) {
/*  71 */           for (int i = readInt(); i > 0; i--) {
/*  72 */             duration.addMessage(readString());
          }
        }
        break;
    }
  }



  public void visitEntity(Entity entity) {
    byte tag;
/*  83 */     while ((tag = readTag()) != -1)
/*  84 */       visitEntityChildren(entity, (tag & 0xFF) >> 2, tag & 0x3); 
  }

  protected void visitEntityChildren(Entity entity, int _field, int _type) {
    GraphTrend graphTrend;
/*  89 */     switch (_field) {
      case 1:
/*  91 */         entity.setId(readString());
        break;
      case 2:
/*  94 */         entity.setType(readString());
        break;
      case 3:
/*  97 */         entity.setStatus(readString());
        break;
      case 33:
/* 100 */         if (_type == 1) {
/* 101 */           JavaThread thread = new JavaThread();

/* 103 */           visitThread(thread);
/* 104 */           this.m_linker.onThread(entity, thread); break;
/* 105 */         }  if (_type == 2) {
/* 106 */           for (int i = readInt(); i > 0; i--) {
/* 107 */             JavaThread thread = new JavaThread();

/* 109 */             visitThread(thread);
/* 110 */             this.m_linker.onThread(entity, thread);
          }
        }
        break;
      case 34:
/* 115 */         if (_type == 1) {
/* 116 */           Duration duration = new Duration();

/* 118 */           visitDuration(duration);
/* 119 */           this.m_linker.onDuration(entity, duration); break;
/* 120 */         }  if (_type == 2) {
/* 121 */           for (int i = readInt(); i > 0; i--) {
/* 122 */             Duration duration = new Duration();

/* 124 */             visitDuration(duration);
/* 125 */             this.m_linker.onDuration(entity, duration);
          }
        }
        break;
      case 35:
/* 130 */         graphTrend = new GraphTrend();

/* 132 */         visitGraphTrend(graphTrend);
/* 133 */         this.m_linker.onGraphTrend(entity, graphTrend);
        break;
    }
  }



  public void visitEntry(Entry entry) {
    byte tag;
/* 142 */     while ((tag = readTag()) != -1) {
/* 143 */       visitEntryChildren(entry, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitEntryChildren(Entry entry, int _field, int _type) {
/* 148 */     switch (_field) {
      case 1:
/* 150 */         entry.setType(readString());
        break;
      case 2:
/* 153 */         entry.setStatus(readString());
        break;
      case 33:
/* 156 */         if (_type == 1) {
/* 157 */           Duration duration = new Duration();

/* 159 */           visitDuration(duration);
/* 160 */           this.m_linker.onDuration(entry, duration); break;
/* 161 */         }  if (_type == 2) {
/* 162 */           for (int i = readInt(); i > 0; i--) {
/* 163 */             Duration duration = new Duration();

/* 165 */             visitDuration(duration);
/* 166 */             this.m_linker.onDuration(entry, duration);
          }
        }
        break;
      case 34:
/* 171 */         if (_type == 1) {
/* 172 */           JavaThread thread = new JavaThread();

/* 174 */           visitThread(thread);
/* 175 */           this.m_linker.onThread(entry, thread); break;
/* 176 */         }  if (_type == 2) {
/* 177 */           for (int i = readInt(); i > 0; i--) {
/* 178 */             JavaThread thread = new JavaThread();

/* 180 */             visitThread(thread);
/* 181 */             this.m_linker.onThread(entry, thread);
          }
        }
        break;
    }
  }



  public void visitGraphTrend(GraphTrend graphTrend) {
    byte tag;
/* 192 */     while ((tag = readTag()) != -1) {
/* 193 */       visitGraphTrendChildren(graphTrend, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitGraphTrendChildren(GraphTrend graphTrend, int _field, int _type) {
/* 198 */     switch (_field) {
      case 1:
/* 200 */         graphTrend.setDuration(readInt());
        break;
      case 2:
/* 203 */         graphTrend.setFails(readString());
        break;
    }
  }



  public void visitMachine(Machine machine) {
    byte tag;
/* 212 */     while ((tag = readTag()) != -1) {
/* 213 */       visitMachineChildren(machine, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMachineChildren(Machine machine, int _field, int _type) {
/* 218 */     switch (_field) {
      case 1:
/* 220 */         machine.setIp(readString());
        break;
      case 33:
/* 223 */         if (_type == 1) {
/* 224 */           Entry entry = new Entry();

/* 226 */           visitEntry(entry);
/* 227 */           this.m_linker.onEntry(machine, entry); break;
/* 228 */         }  if (_type == 2) {
/* 229 */           for (int i = readInt(); i > 0; i--) {
/* 230 */             Entry entry = new Entry();

/* 232 */             visitEntry(entry);
/* 233 */             this.m_linker.onEntry(machine, entry);
          }
        }
        break;
      case 34:
/* 238 */         if (_type == 1) {
/* 239 */           Entity entity = new Entity();

/* 241 */           visitEntity(entity);
/* 242 */           this.m_linker.onEntity(machine, entity); break;
/* 243 */         }  if (_type == 2) {
/* 244 */           for (int i = readInt(); i > 0; i--) {
/* 245 */             Entity entity = new Entity();

/* 247 */             visitEntity(entity);
/* 248 */             this.m_linker.onEntity(machine, entity);
          }
        }
        break;
    }
  }



  public void visitProblemReport(ProblemReport problemReport) {
    byte tag;
/* 259 */     if ((tag = readTag()) != -4) {
/* 260 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 263 */     while ((tag = readTag()) != -1) {
/* 264 */       visitProblemReportChildren(problemReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitProblemReportChildren(ProblemReport problemReport, int _field, int _type) {
/* 269 */     switch (_field) {
      case 1:
/* 271 */         problemReport.setDomain(readString());
        break;
      case 2:
/* 274 */         problemReport.setStartTime(readDate());
        break;
      case 3:
/* 277 */         problemReport.setEndTime(readDate());
        break;
      case 4:
/* 280 */         if (_type == 1) {
/* 281 */           problemReport.addDomain(readString()); break;
/* 282 */         }  if (_type == 2) {
/* 283 */           for (int i = readInt(); i > 0; i--) {
/* 284 */             problemReport.addDomain(readString());
          }
        }
        break;
      case 5:
/* 289 */         if (_type == 1) {
/* 290 */           problemReport.addIp(readString()); break;
/* 291 */         }  if (_type == 2) {
/* 292 */           for (int i = readInt(); i > 0; i--) {
/* 293 */             problemReport.addIp(readString());
          }
        }
        break;
      case 33:
/* 298 */         if (_type == 1) {
/* 299 */           Machine machine = new Machine();

/* 301 */           visitMachine(machine);
/* 302 */           this.m_linker.onMachine(problemReport, machine); break;
/* 303 */         }  if (_type == 2) {
/* 304 */           for (int i = readInt(); i > 0; i--) {
/* 305 */             Machine machine = new Machine();

/* 307 */             visitMachine(machine);
/* 308 */             this.m_linker.onMachine(problemReport, machine);
          }
        }
        break;
    }
  }



  public void visitSegment(Segment segment) {
    byte tag;
/* 319 */     while ((tag = readTag()) != -1) {
/* 320 */       visitSegmentChildren(segment, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitSegmentChildren(Segment segment, int _field, int _type) {
/* 325 */     switch (_field) {
      case 1:
/* 327 */         segment.setId(Integer.valueOf(readInt()));
        break;
      case 2:
/* 330 */         segment.setCount(readInt());
        break;
      case 3:
/* 333 */         if (_type == 1) {
/* 334 */           segment.addMessage(readString()); break;
/* 335 */         }  if (_type == 2) {
/* 336 */           for (int i = readInt(); i > 0; i--) {
/* 337 */             segment.addMessage(readString());
          }
        }
        break;
    }
  }



  public void visitThread(JavaThread thread) {
    byte tag;
/* 348 */     while ((tag = readTag()) != -1) {
/* 349 */       visitThreadChildren(thread, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitThreadChildren(JavaThread thread, int _field, int _type) {
/* 354 */     switch (_field) {
      case 1:
/* 356 */         thread.setGroupName(readString());
        break;
      case 2:
/* 359 */         thread.setName(readString());
        break;
      case 3:
/* 362 */         thread.setId(readString());
        break;
      case 33:
/* 365 */         if (_type == 1) {
/* 366 */           Segment segment = new Segment();

/* 368 */           visitSegment(segment);
/* 369 */           this.m_linker.onSegment(thread, segment); break;
/* 370 */         }  if (_type == 2) {
/* 371 */           for (int i = readInt(); i > 0; i--) {
/* 372 */             Segment segment = new Segment();

/* 374 */             visitSegment(segment);
/* 375 */             this.m_linker.onSegment(thread, segment);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 384 */       return new Date(readVarint(64));
/* 385 */     } catch (IOException e) {
/* 386 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 392 */       return (int)readVarint(32);
/* 393 */     } catch (IOException e) {
/* 394 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 400 */       return this.m_in.readUTF();
/* 401 */     } catch (IOException e) {
/* 402 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 408 */       return this.m_in.readByte();
/* 409 */     } catch (IOException e) {
/* 410 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 415 */     int shift = 0;
/* 416 */     long result = 0L;

/* 418 */     while (shift < length) {
/* 419 */       byte b = this.m_in.readByte();
/* 420 */       result |= (b & 0x7F) << shift;
/* 421 */       if ((b & 0x80) == 0) {
/* 422 */         return result;
      }
/* 424 */       shift += 7;
    }

/* 427 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/problem/model/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */